home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Periodicals / develop / develop 4 code / Poly. in Code Resources / Virtual WDEF / IconWDEF.cp < prev    next >
Encoding:
Text File  |  1990-07-28  |  5.0 KB  |  183 lines  |  [TEXT/MPS ]

  1. /*
  2.     IconWDEF.cp
  3.     
  4.     Implementation of Icon window definition class methods.
  5.     
  6.     by Patrick Beard.
  7.     
  8.     ©1990 by Patrick C. Beard.  All rights reserved.
  9.  */
  10.  
  11. #ifndef __ICONWDEF__
  12. #include "IconWDEF.h"
  13. #endif
  14.  
  15. #include <Events.h>
  16.  
  17. // bitmap for the iconified view.
  18.  
  19. static unsigned short IconBits[] = {
  20.     0x3FFF, 0xFFFF, 0x2000, 0x0001, 0xFFFF, 0xFFFF, 0xE000, 0x0001,
  21.     0xFFFF, 0xFFFF, 0xE000, 0x0001, 0xE000, 0x0001, 0xE000, 0x0001,
  22.     0xE000, 0x0001, 0xE000, 0x0001, 0xE000, 0x0001, 0xE000, 0x0001,
  23.     0xE000, 0x0001, 0xE000, 0x0001, 0xE000, 0x0001, 0xE000, 0x0001,
  24.     0xE000, 0x0001, 0xE000, 0x0001, 0xE000, 0x0001, 0xE000, 0x0001,
  25.     0xE000, 0x0001, 0xE000, 0x0001, 0xE000, 0x0001, 0xE000, 0x0001,
  26.     0xE000, 0x0001, 0xE000, 0x0001, 0xE000, 0x0001, 0xE000, 0x0001,
  27.     0xE000, 0x0001, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFC, 0xFFFF, 0xFFFC
  28. };
  29.  
  30. static Boolean OptionKeyDown()
  31. {
  32.     // see if the command key is down.
  33.     KeyMap theKeys;
  34.     GetKeys(theKeys);
  35.     return (theKeys[1] & 4L) != 0;
  36. }
  37.  
  38. // initialize the state of the window.
  39.  
  40. void IconWindowDef::New(WindowPeek theWindow)
  41. {
  42.     WindowFrame::New(theWindow);
  43.     
  44.     iconified = false;
  45.     everIconified = false;
  46.     requestingStateChange = false;
  47.     calcRgnsCount = 0;
  48.  
  49.     GrafPtr oldPort; GetPort(&oldPort);
  50.     SetPort((GrafPtr)itsWindow);
  51.     
  52.     // initialize the default zoom states of the window.
  53.     WStateDataHandle states = (WStateDataHandle)itsWindow->dataHandle;
  54.     if(states) {
  55.         itsWindow->spareFlag = true;                        // zooming enabled.
  56.         Rect r = itsWindow->port.portRect;
  57.         LocalToGlobal(&topLeft(r));
  58.         LocalToGlobal(&botRight(r));
  59.         (**states).userState = r;                            // normal size.
  60.         r.bottom = r.top + 1;
  61.         r.right = r.left + 1;
  62.         (**states).stdState = r;                            // icon size.        
  63.     }
  64.  
  65.     SetPort(oldPort);
  66. }
  67.  
  68. void IconWindowDef::CalcRgns()
  69. {
  70.     GrafPtr oldPort; GetPort(&oldPort);
  71.     SetPort((GrafPtr)itsWindow);
  72.     WStateDataHandle states = (WStateDataHandle)itsWindow->dataHandle;
  73.     Rect r = itsWindow->port.portRect;
  74.     LocalToGlobal(&topLeft(r));
  75.     LocalToGlobal(&botRight(r));
  76.     if(requestingStateChange) {
  77.         if(--calcRgnsCount == 0) {
  78.             requestingStateChange = false;
  79.             iconified = !iconified;
  80.         }
  81.     }
  82.     if(iconified) {
  83.         Rect portRect = itsWindow->port.portRect;
  84.         LocalToGlobal(&topLeft(portRect));
  85.         LocalToGlobal(&botRight(portRect));
  86.         SetEmptyRgn(itsWindow->contRgn);
  87.         portRect.right = portRect.left + 32;
  88.         portRect.bottom = portRect.top + 32;
  89.         RectRgn(itsWindow->strucRgn, &portRect);
  90.         RgnHandle notchRgn = NewRgn();
  91.         SetRectRgn(notchRgn, portRect.left, portRect.top, portRect.left+2, portRect.top+2);
  92.         DiffRgn(itsWindow->strucRgn, notchRgn, itsWindow->strucRgn);
  93.         SetRectRgn(notchRgn, portRect.right-2, portRect.bottom-2, portRect.right, portRect.bottom);
  94.         DiffRgn(itsWindow->strucRgn, notchRgn, itsWindow->strucRgn);
  95.         DisposeRgn(notchRgn);
  96.     } else {
  97.         WindowFrame::CalcRgns();
  98.     }
  99.     SetPort(oldPort);    
  100. }
  101.  
  102. void IconWindowDef::DrawFrame()
  103. {    
  104.     if(!itsWindow->visible)
  105.         return;
  106.  
  107.     if(iconified) {
  108.         // draw the window as an icon.
  109.         GrafPtr currPort; GetPort(&currPort);
  110.         BitMap windIcon = { (Ptr)IconBits, 4, { 0, 0, 32, 32 } };
  111.         Rect iconRect = (**itsWindow->strucRgn).rgnBBox;
  112.         CopyBits(&windIcon, &currPort->portBits, &windIcon.bounds, &iconRect, srcCopy, itsWindow->strucRgn);    
  113.     } else {
  114.         WindowFrame::DrawFrame();
  115.         // draw in zoom, or iconify box.
  116.         if(itsWindow->hilited) {
  117.             // recompute the iconifyRgn.
  118.             iconifyRect = (**itsWindow->strucRgn).rgnBBox;
  119.             short insetAmount = 5 + GetWVariant((WindowPtr)itsWindow);
  120.             iconifyRect.bottom = iconifyRect.top + insetAmount;
  121.             iconifyRect.left = iconifyRect.right - insetAmount;
  122.             InsetRect(&iconifyRect, 1, 1);
  123.             Pattern iconifyPattern = { 0xAA, 0xAB, 0xA8, 0xAF, 0xA0, 0xBF, 0x80, 0xFF };
  124.             FillRect(&iconifyRect, iconifyPattern);
  125.         }
  126.     }
  127. }
  128.  
  129. long IconWindowDef::Hit(Point& whereHit)
  130. {
  131.     GrafPtr currPort; GetPort(&currPort);
  132.     SetPort(&itsWindow->port);
  133.     Rect r = itsWindow->port.portRect;
  134.     LocalToGlobal(&topLeft(r));
  135.     LocalToGlobal(&botRight(r));
  136.     SetPort(currPort);
  137.  
  138.     WStateDataHandle states = (WStateDataHandle)itsWindow->dataHandle;
  139.     if(iconified) {
  140.         (**states).stdState = r;                    // icon size.        
  141.     } else {
  142.         (**states).userState = r;                    // normal size.        
  143.     }
  144.  
  145.     // option-key is used to change state of the window.
  146.     if(OptionKeyDown()) {
  147.         if(iconified) {
  148.             if(!requestingStateChange) {
  149.                 requestingStateChange = true;
  150.                 calcRgnsCount = 2;
  151.             }
  152.             return wInZoomIn;
  153.         } else {
  154.             // recompute the iconifyRgn.
  155.             iconifyRect = (**itsWindow->strucRgn).rgnBBox;
  156.             short insetAmount = 5 + GetWVariant((WindowPtr)itsWindow);
  157.             iconifyRect.bottom = iconifyRect.top + insetAmount;
  158.             iconifyRect.left = iconifyRect.right - insetAmount;
  159.             InsetRect(&iconifyRect, 1, 1);        
  160.             if(PtInRect(whereHit, &iconifyRect)) {
  161.                 // make iconified location same place window is now.
  162.                 if(!everIconified) {
  163.                     r.left = r.right - 32;
  164.                     r.bottom = r.top + 1;
  165.                     (**states).stdState = r;
  166.                     everIconified = true;
  167.                 }
  168.                 if(!requestingStateChange) {
  169.                     requestingStateChange = true;
  170.                     calcRgnsCount = 2;
  171.                 }
  172.                 return wInZoomOut;
  173.             }
  174.         }
  175.     }
  176.     
  177.     // if iconified, we handle hit testing, if not, normal window does.
  178.     if(iconified)
  179.         return wInDrag;
  180.     else
  181.         return WindowFrame::Hit(whereHit);
  182. }
  183.